<template>
  <div class="div_padding">
    <el-form :inline="true" :model="formInline" ref="formInline" class="demo-form-inline" size="small">
      <el-form-item label="菜单名称：" prop="name">
        <el-input v-model="formInline.name" placeholder="菜单名称"></el-input>
      </el-form-item>
      <el-form-item label="组件名称：" prop="component">
        <el-input v-model="formInline.component" placeholder="组件名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" v-permission="'Search'" @click="filterData">查询</el-button>
        <el-button icon="el-icon-refresh" plain @click="resetForm('formInline')">重置</el-button>
        <el-button icon="el-icon-plus" v-permission="'Create'" type="success" @click="onSubmit">添加菜单</el-button>
        <Insert ref="insert" v-bind:menuData="tableData" @reloadTable="reloadTable"></Insert>
        <Edit ref="edit" v-bind:menuData="tableData" @reloadTable="reloadTable"></Edit>
      </el-form-item>
      <el-button style="float: right" icon="el-icon-refresh-right" circle @click="reloadTable"></el-button>
    </el-form>
    <el-table
      :data="tableData"
      size="small"
      height="78vh"
      v-loading="loading"
      empty-text="查询结果为空!"
      element-loading-text="数据加载中..."
      element-loading-spinner="el-icon-loading"
      style="width: 100%;overflow-x: hidden;"
      row-key="id"
      border>
      <el-table-column
        prop="id"
        label="ID"
        v-if="showId"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="菜单名称"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="icon"
        label="菜单图标"
        align="center"
        width="100">
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序"
        sortable
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="is_catalog"
        label="是否目录"
        align="center"
        width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.is_catalog == 0" style="color:#67C23A;">否</span>
          <span v-if="scope.row.is_catalog == 1" style="color:#909399;">是</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="path"
        label="路径"
        align="center"
        width="150">
      </el-table-column>
      <el-table-column
        prop="nocache"
        label="是否缓存"
        align="center"
        width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.nocache == 0" style="color:#67C23A;">不缓存</span>
          <span v-if="scope.row.nocache == 1" style="color:#909399;">缓存</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="hidden"
        label="侧边栏中是否显示"
        align="center"
        width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.hidden == true" style="color:#67C23A;">显示</span>
          <span v-if="scope.row.hidden == false" style="color:#909399;">隐藏</span>
        </template>
      </el-table-column>
      <el-table-column
      prop="component"
      label="组件名称"
      align="center"
      width="100">
    </el-table-column>
      <el-table-column
        prop="menuPermission"
        label="权限"
        align="center"
        width="350">
        <template slot-scope="scope">
          <el-button v-for="(k,v) in scope.row.menuPermission" :key="v.index" size="mini" :type="buttonTypeList[v]">{{ k }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="status" width="100" align="center" label="显示状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status == true" style="color:#67C23A;">正常</span>
          <span v-if="scope.row.status == false" style="color:#909399;">停用</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="update_datetime"
        label="更新时间"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="create_datetime"
        label="创建时间"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作" align="center" width="300" prop="parent" fixed="right">
        <template slot-scope="scope">
            <el-button type="primary" size="mini" v-permission="'Update'" @click="onEdit(scope.$index,scope.row)" icon="el-icon-edit" plain>修改</el-button>
            <el-button type="danger" size="mini" v-permission="'Delete'" icon="el-icon-delete" plain @click="onDelete(scope.row)">删除</el-button>
            <el-button type="warning" size="mini" v-show="scope.row.parent !=null " icon="el-icon-setting" plain @click="onSetting(scope.row)">按钮配置</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
  import { getMenuData,delMenuData } from '@/api/menu'
  import { successMsg } from '@/utils/tools'
  import Insert from './insert'
  import Edit from './edit'
  export default {
    name:'Menu',
    components:{
      Insert,
      Edit
    },
    data() {
      return {
        showId:false,
        loading:true,
        buttonTypeList:[
          'primary',
          'info',
          'success',
          'warning',
          'danger',
          'primary',
          'info',
          'success',
          'warning',
          'danger',
        ],
        tableData: [],
        formInline: {
          name: '',
          component: ''
        }
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        getMenuData().then(res=>{
          this.tableData=this.$utils.toArrayTree(res.data.data,{ parentKey: 'parent', strict: false })
          this.loading=false
        })
      },
      onSubmit(){
        this.$refs.insert.dialogFormVisible = true
      },
      onEdit(index,row){
        this.$refs.edit.dialogFormVisible = true
        this.$refs.edit.formInline = Object.assign({}, row);//formInline，将row的数据复制到editForm对象中，实现的是浅拷贝
      },
      onDelete(row){
        this.$confirm('此操作将真实删除该菜单且为级联删除，谨慎操作!是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
          delMenuData(row.id).then(res=>{
            if(res.code===2000) {
              successMsg(res.msg)
              this.reloadTable()
            }
          })
        }).catch(()=>{
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        })
      },
      reloadTable(){
        this.loading=true
        this.fetchData()
      },
      filterData(){
        let params = {}
        if(this.formInline.name){
          params.name=this.formInline.name
        }
        if(this.formInline.component){
          params.component=this.formInline.component
        }
        this.loading=true
        getMenuData(params).then(res=>{
          this.tableData=this.$utils.toArrayTree(res.data.data,{ parentKey: 'parent', strict: false })
          this.loading=false
        })
      },
      resetForm(formName){
        this.$refs[formName].resetFields()
        this.reloadTable()
      },
      onSetting(row){
        this.$router.push({path:'/system/menuButton',query:{'title':row.name,'id':row.id}})
      },
    },
  }
</script>
<style lang="scss" scoped>
  .div_padding {
    padding: 10px 10px
  }
</style>
